package com.snlab;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;

public class Verifier {
    public static HashMap<Integer, HashSet<Port>> predToPorts;
    private HashMap<String, Device> devices;
    public static HashMap<Integer, Predicate> predicates;

    public Verifier() {
        predToPorts = new HashMap<>();
        predToPorts.put(1, new HashSet<>());
        this.devices = new HashMap<>();
        predicates = new HashMap<>();
        predicates.put(1, new Predicate(1));
    }

    public void addDevice(String name, Device device) {
        this.devices.put(name, device);
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String[] devicenames = {"atla", "chic", "hous", "kans", "losa", "newy32aoa", "salt", "seat", "wash"};
        Verifier verifier = new Verifier();
        for (String name : devicenames) {
            Device d = new Device(name);
            verifier.addDevice(name, d);
        }
        for (String name : devicenames) {
            File inputFile = new File("i2/" + name + "apnotcomp");
            Scanner line = null;
            try {
                line = new Scanner(inputFile);
                line.useDelimiter("\n");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(0);
            }

            while (line.hasNext()) {
                String linestr = line.next();
                String[] tokens = linestr.split(" ");
                if (tokens[0].equals("fw")) {
                    String portname = tokens[3].split("\\.")[0];
                    Port p = verifier.devices.get(name).getPortByName(portname);
                    if (p == null) {
                        p = verifier.devices.get(name).addPortByName(portname);
                    }
                    Rule rule = new Rule(Long.parseLong(tokens[1]), Integer.parseInt(tokens[2]), p);
                    verifier.devices.get(name).addRule(rule);
                }
            }
        }

        long end = System.currentTimeMillis();
        System.out.println("finish");
        System.out.println(end-start);
        System.out.println(Verifier.predToPorts.size());
    }
}
